package com.css.ztb.service.impl;

import com.css.ztb.common.Constants;
import com.css.ztb.domain.SysConfig;
import com.css.ztb.mapper.SysConfigMapper;
import com.css.ztb.service.SysConfigService;
import com.css.ztb.util.CacheUtils;
import com.css.ztb.util.ConvertUtils;
import com.css.ztb.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shipeilei
 * @date 2020-6-26 16:22:50
 * @desc desc
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public String selectConfigByKey(String configKey) {
        String configVal = ConvertUtils.toStr(CacheUtils.get(getCacheName(), getCacheKey(configKey)));
        if (StringUtils.isNotEmpty(configVal)) {
            return configVal;
        }
        SysConfig sysConfig = sysConfigMapper.selectConfigByKey(configKey);
        if (StringUtils.isNotNull(sysConfig)) {
            String configValue = sysConfig.getConfigValue();
            CacheUtils.put(getCacheName(), getCacheKey(configKey), configValue);
            return configValue;
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取cache name
     *
     * @return 缓存名
     */
    private String getCacheName()
    {
        return Constants.SYS_CONFIG_CACHE;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return Constants.SYS_CONFIG_KEY + configKey;
    }
}
